package com.example.demo.service;

/**
 * @author qinbo
 * @version 1.0
 * @description: TODO
 * @date 2025/2/11 23:32
 */

import com.example.demo.utils.Base64Util;
import com.example.demo.utils.GetToken;
import com.example.demo.utils.HttpUtil;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.net.URLEncoder;

@Service
public class OcrService {
    @Value("${baidu.API_URL}")
    private String ocrUrl;
    @Value("${baidu.API_KEY}")
    private String clientId ;
    @Value("${baidu.SECRET_KEY}")
    private String clientSecret ;

    public String generalBasic(byte[] imageData) throws Exception {
        String imgStr = Base64Util.encode(imageData);
        String imgParam = URLEncoder.encode(imgStr, "UTF-8");
        String param = "image=" + imgParam;

        String accessToken = GetToken.getAuth(clientId, clientSecret);
        return HttpUtil.post(ocrUrl, accessToken, param);
    }
}
